/**
 * Created by lichengming on 2017/7/26.
 */
(function($){
    var that = null;
    $.fn.extend(
        {
            "createTable" : function (options) {
                that = $(this);
                options = $.extend({
                    showColumns: true,
                    showRefresh: true,
                    showToggle: true,
                    search: true,
                    sidePagination: "server",
                    pagination: true,
                    clickToSelect: true,
                    pageList: [5, 10, 20, 50, 100, 200],
                    paginationPreText: "上一页",
                    paginationNextText: "下一页",
                    paginationFirstText: "首页",
                    paginationLastText: "尾页",
                    queryParams:queryParams,
                    toolbar: "#table_tools",

                    onPostHeader: function () {
                        $("th").css({"background-color": "#f5f5f5"});
                    }
                },options || {});
                $(this).bootstrapTable(options);
            }
        }
    )

    $(function () {
        //重置
        $(".btn-reset").on("click",function () {
            $("form").find("input,select").each(function (index,val) {
                $(val).val("");
            });
        });


        //搜索
        $(".btn-search").on("click",function () {
            that.bootstrapTable('refresh', getQueryParams());
        });
    })
})(jQuery);

function queryParams(params) {
    params.pageSize = this.pageSize;
    params.pageNumber = this.pageNumber;

    $("form").find("input,select").each(function (index,val) {
        params[$(val).attr("name")] = $(val).val();
    });
    return params;
}

function getQueryParams() {
    var params = {};
    $("form").find("input,select").each(function (index,val) {
        params[$(val).attr("name")] = $(val).val();
    });
    return params;
}

function search(target,params) {
    var queryParams = {"query":params};
    $(target).bootstrapTable('refresh', queryParams);
}